﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Markup;
using System.Globalization;
using Microsoft.Advertising.Mobile.UI;
using System.Xml.Linq;
using System.Windows.Navigation;
using System.ComponentModel;
using Common.Library;
using OnlineVideos.Library;
using OnlineVideos.UI;
using OnlineVideos.Data;
using OnlineVideos.ViewModels;
using OnlineVideos.Entities;
using Common.Utilities;

namespace OnlineVideos
{
    public partial class HistoryPivot : PhoneApplicationPage
    {
        #region Global
        private SolidColorBrush adcontrolborder = new SolidColorBrush(Colors.Transparent);

        History objHistory = new History();
      
        string link = string.Empty;
        int movieLastIndex = 1;
        int movieFirstIndex = 1;
        int songLastIndex = 1;
        int songFirstIndex = 1;
        int moviesHisCount = 0;
        int songsHisCount = 0;
        bool newPageInstance = false;

        //ListBox lbxVidoes;

        #endregion

        public HistoryPivot()
        {
            InitializeComponent();
            newPageInstance = true;
        }
       
       

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("HistoryPivot Page");
                LoadPivotThemes();
                LoadSongHistory();
                
                if (newPageInstance)
                {
                    LoadVideoHistory();
                }

                newPageInstance = false;
                if (pvtMainHistory.Items.Count >= 2)
                    pvtMainHistory.SelectedIndex =0;
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In HistoryPivot.cs file.", ex);
            }
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("HistoryPivot Page");
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In HistoryPivot.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In HistoryPivot.cs file.", ex);
            }
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
          

            base.OnBackKeyPress(e);
        }

        private void lbxSongHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxSongHistory.SelectedIndex == -1)
                    return;

                YoutubeApi.Youtube.PlayYoutubeVideo((lbxSongHistory.SelectedItem as ShowLinks).LinkUrl);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxSongHistory_SelectionChanged Method In HistoryPivot.cs file.", ex);
            }
            lbxSongHistory.SelectedIndex = -1;
        }

        private void LoadVideoHistory()
        {
            try
            {
                if (!AppResources.ShowFavoritesPageMoviesPivot)
                {
                    pvtitmVideos.Visibility = Visibility.Collapsed;
                    pvtMainHistory.Items.Remove(pvtitmVideos);
                }
                else
                {
                    pvtitmVideos.Visibility = Visibility.Visible;

                    pvtitmVideos.Header = AppResources.FavoriteMoviesPivotTitle;
                    movieLastIndex = objHistory.GetLastHisId(Constants.MovieHistoryFile);
                    movieFirstIndex = movieFirstIndex - 14;
                    if (movieFirstIndex < 0)
                        movieFirstIndex = 1;
                    moviesHisCount = 15;
                    List<LinkHistory> objmovie = objHistory.LoadMovieHistory(movieFirstIndex, movieLastIndex, moviesHisCount);
                    if (objmovie.Count > 0)
                        lbxVidoes.ItemsSource = objmovie;
                    else
                        tblk.Text = "No " + AppResources.FavoriteMoviesPivotTitle + " available";
                    tblk.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadVideoHistory Method In HistoryPivot.cs file.", ex);
            }
        }

        void lbxVidoes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxVidoes.SelectedIndex == -1)
                    return;
                if (!AppResources.ShowFavoritesPageMoviesPivot)
                {
                    List<ShowLinks> objsong =(OnlineShow.GetShowLinksByType((lbxVidoes.SelectedItem as ShowLinks).ShowID.ToString(), LinkType.Movies)).ToList();
                    foreach (ShowLinks objmovie in objsong)
                    {
                        link = objmovie.LinkUrl;
                    }
                    YoutubeApi.Youtube.PlayYoutubeVideo(link);
                   
                }
                else
                {
                    if (!AppResources.IsSimpleDetailPage)
                        NavigationService.Navigate(NavigationHelper.getMovieDetailPage((lbxVidoes.SelectedItem as ShowLinks).ShowID.ToString()));
                    else
                        NavigationService.Navigate(NavigationHelper.getSongDetailPage((lbxVidoes.SelectedItem as ShowLinks).ShowID.ToString()));
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxVidoes_SelectionChanged Method In HistoryPivot.cs file.", ex);
            }
            lbxVidoes.SelectedIndex = -1;
        }

        private void LoadSongHistory()
        {
            try
            {
                songLastIndex = objHistory.GetLastHisId(Constants.SongHistoryFile);
                songFirstIndex = songFirstIndex - 14;
                if (songFirstIndex < 0)
                    songFirstIndex = 1;
                songsHisCount = 15;
                List<LinkHistory> objsong = objHistory.LoadSongHistory(songFirstIndex, songLastIndex, songsHisCount);
                if (objsong.Count > 0)
                {
                    lbxSongHistory.ItemsSource = objsong;
                    tblkFavNoSongs.Visibility = Visibility.Collapsed;
                }
                else
                    tblkFavNoSongs.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadSongHistory Method In HistoryPivot.cs file.", ex);
            }
        }

        private void LoadPivotThemes()
        {
            try
            {
                if (AppResources.ShowDetailPageAudioVideoHeaders)
                {
                    pvtitemSongs.Header = AppResources.FavoriteSongsPivotTitle;
                }
                else
                    pvtitemSongs.Header = "";
                tblkFavNoSongs.Text = "No " + AppResources.FavoriteSongsPivotTitle + " available";
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadPivotThemes Method In HistoryPivot.cs file.", ex);
            }
        }

        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
    }
}